import { _decorator, BoxCollider2D, CircleCollider2D, Component, director, EventTouch, Input, instantiate, log, Node, PolygonCollider2D, Prefab, RigidBody2D, Sprite } from 'cc';
import { LayerRootAction } from './LayerRootAction';
import { Screen } from "cc";
import { Global } from './Global';
import { LayerCatAction } from './LayerCatAction';
import { LayerEmptyAction } from './LayerEmptyAction';
import { Clips, events } from './Enums';
import { AudioManger } from './AudioManger';
import { CatAction } from './CatAction';
import { MenuAction } from './MenuAction';
import { ShowLevelAction } from './ShowLevelAction';
import { GameOverAction } from './GameOverAction';
import { CoinsAction } from './CoinsAction';
import { SuccessAction } from './SuccessAction';
import { LayerBtnAction } from './LayerBtnAction';
import { TipsAction } from './TipsAction';
import { BucketAction } from './BucketAction';
import { PropsAction } from './PropsAction';
import { DjManger } from './DjManger';
import { GuideAction } from './GuideAction';
import { LvLStorage } from './LvLStorage';
const { ccclass, property } = _decorator;

@ccclass('Main')
export class Main extends Component {

    @property({type:LayerRootAction})
    layer_root:LayerRootAction
    @property({type:LayerCatAction})
    layer_cat:LayerCatAction
    @property({type:LayerEmptyAction})
    layer_empty:LayerEmptyAction
    @property({type:ShowLevelAction})
    Show_level_action:ShowLevelAction

    @property({type:MenuAction})
    menu:MenuAction
    @property({type:GameOverAction})
    game_over_action:GameOverAction
    @property({type:CoinsAction})
    coins_action:CoinsAction
    @property({type:SuccessAction})
    success_action:SuccessAction
    @property({type:LayerBtnAction})
    layer_btn_action:LayerBtnAction
    @property({type:TipsAction})
    tips_action:TipsAction;

    @property({type:BucketAction})
    bucket_action:BucketAction;

    @property({type:PropsAction})
    props_action:PropsAction;

    @property({type:GuideAction})
    guide_action:GuideAction;

    

    // @property({type:Number,tooltip:"默认开始等级，一般填写1，如果填写3就是从第三关开始"})
    // default_start_lvl:number;

    @property({type:Number,tooltip:"基本扳手消耗，真是消耗是 lvl*need_coins"})
    need_coins:number;
    
    
    
    protected onLoad(): void {
        director.on(events.check_completed, this.next_lvlcheck_completed, this);
    }

    start() {
        //set default need coin
       Global.need_coins = this.need_coins;
       //
       Global.layer_root_action = this.layer_root;
       Global.layer_cat_action = this.layer_cat;
       Global.layer_empty_action = this.layer_empty;
       Global.main_action = this;
       Global.Show_level_action = this.Show_level_action;
       Global.game_over_action = this.game_over_action;
       Global.coins_action = this.coins_action;
       Global.success_action = this.success_action;
       Global.layer_btn_action = this.layer_btn_action;
       Global.tips_action = this.tips_action;
       Global.bucket_action = this.bucket_action; 
       Global.props_action = this.props_action; 
       Global.guide_action = this.guide_action;
       this.restart_game();
    }

    //从头开始游戏，1..max
    restart_game(){
        let lvl = LvLStorage.get_cur_lvl();
        Global.restart_default(lvl); // 3
        this.start_lvl(lvl);
    }

    start_lvl(_lvl:number){
        Global.is_check_completed = true;
        //记录等级信息
        LvLStorage.record_cur_lvl(_lvl);
        //重置 钉子,金币
        Global.every_level_default();
        DjManger.reset_dj_cur_();
        console.log("开始游戏  》〉》〉》〉 level >>>>",_lvl);
        this.layer_root.init_root(_lvl); 
        Global.Show_level_action.show_level();
        Global.coins_action.refrush_coins();
        // Global.layer_root_action.random_pin();
        Global.is_check_completed = false;
    }

    next_lvlcheck_completed(catAction:CatAction){
        // Global.success_action.open();return;
        if(Global.cur_lvl_pin_move_num>= Global.cur_lvl_pin_total){
            // console.log(" check is completed  success");
            Global.success_action.open();
        }else{
            catAction.into_scence();
        }
    }

    

    btn_open_menu(){
        AudioManger.instance.playSound(Clips.btn_1);
        this.menu.open();
    }

    update(deltaTime: number) {
        
    }


   
}

